package com.dgut.admodule.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.dgut.admodule.entity.Company;

public class CompanyQueryDTO {
	private Long cId;
	private String cName;
	private String cDescription;
	private String cPhone;
	private String cEmail;
	private String cAdress;
	private String userName;
	
	public Long getcId() {
		return cId;
	}
	public void setcId(Long cId) {
		this.cId = cId;
	}
	public String getcName() {
		return cName;
	}
	public void setcName(String cName) {
		this.cName = cName;
	}
	public String getcDescription() {
		return cDescription;
	}
	public void setcDescription(String cDescription) {
		this.cDescription = cDescription;
	}
	public String getcPhone() {
		return cPhone;
	}
	public void setcPhone(String cPhone) {
		this.cPhone = cPhone;
	}
	public String getcEmail() {
		return cEmail;
	}
	public void setcEmail(String cEmail) {
		this.cEmail = cEmail;
	}
	public String getcAdress() {
		return cAdress;
	}
	public void setcAdress(String cAdress) {
		this.cAdress = cAdress;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public static Specification<Company> getSpecification(CompanyQueryDTO companyQueryDTO)
	{
		Specification<Company> spec = new Specification<Company>() {
			public Predicate toPredicate(Root<Company> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != companyQueryDTO && !StringUtils.isEmpty(companyQueryDTO.getcId())) {
					 Predicate  p1 =  cb.equal(root.get("cId"),companyQueryDTO.getcId());
					 list.add(p1);
				 }
				 if(null != companyQueryDTO && !StringUtils.isEmpty(companyQueryDTO.getcName())) {
					 Predicate  p2 =  cb.like(root.get("cName").as(String.class),"%"+ companyQueryDTO.getcName() + "%");
					 list.add(p2);
				 }
				 if(null != companyQueryDTO && !StringUtils.isEmpty(companyQueryDTO.getcDescription())) {
					 Predicate  p3 =  cb.like(root.get("cDescription").as(String.class),"%"+ companyQueryDTO.getcDescription() + "%");
					 list.add(p3);
				 }
				 if(null != companyQueryDTO && !StringUtils.isEmpty(companyQueryDTO.getcAdress())) {
					 Predicate  p4 =  cb.like(root.get("cAdress").as(String.class),"%"+ companyQueryDTO.getcAdress() + "%");
					 list.add(p4);
				 }
				 
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
}
